package com.xueyi.rebar.delivery.service.impl;

import cn.iocoder.yudao.module.bpm.api.task.dto.ApiBpmProcessInstanceCreateReqKeyVO;
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqVO;
import cn.iocoder.yudao.module.bpm.api.util.FormFlowFieldUtils;
import cn.iocoder.yudao.module.bpm.dal.dataobject.BpmProcessInstanceExtDO;
import cn.iocoder.yudao.module.bpm.feign.BpmWfFlowApi;
import com.xueyi.common.core.pojo.CommonResult;
import com.xueyi.common.web.entity.service.impl.BaseServiceImpl;
import com.xueyi.rebar.contant.FlowCodeEnum;
import com.xueyi.rebar.delivery.domain.dto.RebarDistributionDeliveryOrderDto;
import com.xueyi.rebar.delivery.manager.RebarDistributionDeliveryOrderManager;
import com.xueyi.rebar.delivery.mapper.RebarDistributionDeliveryOrderMapper;
import com.xueyi.rebar.delivery.service.IRebarDistributionDeliveryOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 配送单管理 服务层处理
 *
 * @author xueyi
 */
@Service
public class RebarDistributionDeliveryOrderServiceImpl extends BaseServiceImpl<RebarDistributionDeliveryOrderDto, RebarDistributionDeliveryOrderManager, RebarDistributionDeliveryOrderMapper> implements IRebarDistributionDeliveryOrderService {

    @Resource
    private BpmWfFlowApi wfFlowApi;

    @Resource
    private RebarDistributionDeliveryOrderMapper mapper;
    /**
     * 查询配送单对象列表 | 数据权限 | 附加数据
     *
     * @param distributionDeliveryOrder 配送单对象
     * @return 配送单对象集合
     */
    @Override
    //@DataScope(userAlias = "createBy", mapperScope = {"RebarDistributionDeliveryOrderMapper"})
    public List<RebarDistributionDeliveryOrderDto> selectListScope(RebarDistributionDeliveryOrderDto distributionDeliveryOrder) {
        return baseManager.selectListExtra(distributionDeliveryOrder);
    }

    @Override
    public String submit(RebarDistributionDeliveryOrderDto distributionDeliveryOrder) {
        ApiBpmProcessInstanceCreateReqKeyVO instanceCreateReqVO = new ApiBpmProcessInstanceCreateReqKeyVO();
        Map<String, Object> formFieldList = FormFlowFieldUtils.getFormFieldList(distributionDeliveryOrder, null);
        instanceCreateReqVO.setVariables(formFieldList);
        instanceCreateReqVO.setDefinitionkey(FlowCodeEnum.DELIVERY_ORDER.getCode());
        CommonResult<Object> result = wfFlowApi.createProcessInstance(instanceCreateReqVO);
        String processInstanceId = (String) result.getData();
        return processInstanceId;
    }
    /**
     * @description     审批回调方法
     * @author          zhengchao
     * @date            Created in 2022/6/17
     */
    @Override
    public void approvalCallBack(BpmProcessInstanceExtDO resultEvent){
        Integer auditResult = resultEvent.getResult();
        String InstanceId = resultEvent.getProcessInstanceId();
        RebarDistributionDeliveryOrderDto rebarDistributionDeliveryOrderDto = new RebarDistributionDeliveryOrderDto();
        rebarDistributionDeliveryOrderDto.setCurrentFlowInstanceId(InstanceId);
        rebarDistributionDeliveryOrderDto.setAuditStatus(auditResult);
        this.baseManager.updateByCurrentFlowInstanceId(rebarDistributionDeliveryOrderDto);
    }

}